package exception;

public class Student {
    private String name;//姓名
    private  int age;//年龄

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    //设置年龄
    public void setAge(int age) throws  AgeExcepyion{//加throws AgeExcepyion是在方法继续抛出异常，谁调用它就谁处理
        if (age<0)throw  new AgeExcepyion();//抛出异常
        this.age = age;
    }
    public  static  void main(String[] args){
        Student s=new Student();
        //s.setAge(3); /*因为年龄方法中只是抛出异常而没有处理异常，所有这里调用是需要处理异常*/
        try {
            s.setAge(-3);
        } catch (AgeExcepyion ageExcepyion) {
            //ageExcepyion.printStackTrace();
            System.out.println(ageExcepyion.getMessage());
        }
    }
}
